import json

import requests
import asyncio
import aiohttp
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
    'referer':'https://www.taptap.cn/top/download?'
}
# url = 'https://www.taptap.cn/webapiv2/app-top/v2/hits?from=130&limit=10&type_name=hot&X-UA=V%3D1%26PN%3DWebApp%26LANG%3Dzh_CN%26VN_CODE%3D102%26LOC%3DCN%26PLT%3DPC%26DS%3DAndroid%26UID%3D5cf68e57-de8e-4536-af41-86dab4c36f68%26OS%3DWindows%26OSV%3D10%26DT%3DPC'
# res = requests.get(url, headers=headers)
# for item in res.json()['data']['list']:
#     print(item)


# async def fetch(session, url):
#     async with session.get(url) as response:
#         return await response.json()
#
# async def main():
#     async with aiohttp.ClientSession(headers = headers) as session:
#         urls = (f'https://www.taptap.cn/webapiv2/app-top/v2/hits?from={count}&limit=10&type_name={name}&X-UA=V%3D1%26PN%3DWebApp%26LANG%3Dzh_CN%26VN_CODE%3D102%26LOC%3DCN%26PLT%3DPC%26DS%3DAndroid%26UID%3D5cf68e57-de8e-4536-af41-86dab4c36f68%26OS%3DWindows%26OSV%3D10%26DT%3DPC'for count in range(0, 130,10) for name in ['hot', 'reserve', 'pop','new','game_jam'])
#         result = [fetch(session, url) for url in urls]
#         result = await asyncio.gather(*result)
#         total_games = sum(
#             len(data['data']['list']) for data in result if data and 'data' in data and 'list' in data['data'])
#
#         print(f"API响应数量: {len(result)}")
#         print(f"总游戏数量: {total_games}")
#         print(len(result))
#         for data in result:
#             for item in data['data']['list']:
#                 print(item)
#
# asyncio.run(main())

from concurrent.futures import ThreadPoolExecutor


def fetch(url):
    try:
        response = requests.get(url, headers=headers)
        return response.json()
    except Exception as e:
        print(f"请求失败 {url}: {e}")
        return None


def main():
    urls = [
        f'https://www.taptap.cn/webapiv2/app-top/v2/hits?from={count}&limit=10&type_name={name}&X-UA=V%3D1%26PN%3DWebApp%26LANG%3Dzh_CN%26VN_CODE%3D102%26LOC%3DCN%26PLT%3DPC%26DS%3DAndroid%26UID%3D5cf68e57-de8e-4536-af41-86dab4c36f68%26OS%3DWindows%26OSV%3D10%26DT%3DPC'
        for count in range(0, 130, 10) for name in ['hot', 'reserve', 'pop', 'new', 'game_jam','in_app_event_reserve','exclusive','action','strategy','idle']]

    print(f"总URL数量: {len(urls)}")

    with ThreadPoolExecutor(max_workers=10) as executor:
        results = list(executor.map(fetch, urls))

    # 过滤掉None值
    valid_results = [data for data in results if data is not None]
    total_games = sum(len(data['data']['list']) for data in valid_results if 'data' in data and 'list' in data['data'])

    print(f"成功响应数量: {len(valid_results)}")
    print(f"总游戏数量: {total_games}")

    for data in valid_results:
        if 'data' in data and 'list' in data['data']:
            for item in data['data']['list']:
                print(item)


if __name__ == "__main__":
    main()

